package licode.unisop.authcenter.application.executor;

import licode.unisop.authcenter.application.info.RealmUserGroupsRemoveFromUserIn;
import licode.unisop.authcenter.application.info.RealmUsersJoinToGroupIn;
import licode.unisop.authcenter.exception.CenterErrorCode;
import licode.unisop.client.utils.StringUtil;
import licode.unisop.platform.client.api.SiPlatformQueryService;
import licode.unisop.platform.client.api.SiPlatformService;
import licode.unisop.provider.info.*;
import licode.unisop.provider.vo.SiAccountSet;
import licode.unisop.provider.vo.SiPlatform;
import licode.unisop.realm.client.api.SiUserService;
import licode.unisop.realm.client.info.SiResetPasswordIn;
import licode.unisop.realm.client.info.SiUpdatePasswordIn;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 定义统一认证平台中心领域用户相关接口
 *
 * @author licode
 */
@Service
public class CenterRealmUserExecutor {
    @Resource
    private SiUserService siUserService;

    @Resource
    private SiPlatformService siPlatformService;

    @Resource
    private SiPlatformQueryService siPlatformQueryService;

    /**
     * 把领域用户分组分配给平台
     */
    public void assignGroupsToPlatform(SiUserGroupsAssignToPlatIn req) {
        siUserService.assignGroupsToPlatform(req);
    }

    public void saveGroupsOfPlatform(SiUserGroupsAssignToPlatIn req) {
        SiPlatform platform = siPlatformQueryService.lookupPlatform(SiPlatIdFind
                .builder().platformId(req.getPlatformId()).build());
        if (StringUtil.isBlank(req.getRealmId())) {
            throw CenterErrorCode.REALM_ID_IS_BLANK.buildException();
        }
        if (StringUtil.isBlank(req.getPlatformId())) {
            throw CenterErrorCode.PLATFORM_ID_IS_BLANK.buildException();
        }

        if (null != platform) {
            // 需要清除之前分配给平台的领域分组信息
            if (StringUtil.isNotBlank(platform.getRealmId())) {
                siUserService.clearGroupsOfPlatform(SiRealmGroupsOfPlatClearIn
                        .builder()
                        .platformId(req.getPlatformId())
                        .realmId(platform.getRealmId())
                        .build());
            }

            siUserService.saveGroupsOfPlatform(req);

            siPlatformService.updatePlatformRealm(SiPlatRealmUpdateIn.builder()
                    .realmId(req.getRealmId())
                    .platformId(req.getPlatformId())
                    .build());
        }
    }

    public void updateCache(SiUserId userId) {
        siUserService.updateCache(userId);
    }

    public void updatePassword(SiUpdatePasswordIn info) {
        siUserService.updatePassword(info);
    }

    public void resetPassword(SiResetPasswordIn info) {
        siUserService.resetPassword(info);
    }

    public void saveAccount(SiAccountSet info) {
        siUserService.saveAccountSet(info);
    }

    public void assignPlatsToUserGroup(SiPlatsAssignToUserGroupIn req) {
        siUserService.assignPlatsToUserGroup(req);
    }

    /**
     * 移除分配给平台的领域用户分组
     */
    public void removeGroupFromPlatform(UserGroupRemoveFromPlatIn req) {
        long count = siUserService.removeGroupFromPlatform(req);
        if (count == 0) {
            siPlatformService.updatePlatformRealm(SiPlatRealmUpdateIn
                    .builder()
                    .realmId("")
                    .platformId(req.getPlatformId())
                    .build());
        }
    }

    /**
     * 把用户加入指定用户分组
     */
    public void userJoinToGroup(RealmUsersJoinToGroupIn req) {
        siUserService.assignGroupToUsers(SiGroupAssignToUsersIn.builder()
                .realmId(req.getRealmId())
                .groupId(req.getGroupId())
                .userIds(req.getUsers())
                .build());
    }

    /**
     * 把领域用户分组分配给指定用户
     */
    public void groupAssignToUser(SiGroupAssignToUsersIn req) {
        siUserService.assignGroupToUsers(req);
    }

    /**
     * 把用户从分组中移除
     */
    public void userRemoveFromGroup(UsersRemoveFromGroupIn req) {
        siUserService.removeUsersFromGroup(req);
    }

    /**
     * 把分组列表从指定用户中移除
     */
    public void groupRemoveFromUser(RealmUserGroupsRemoveFromUserIn req) {

    }
}
